package com.xique.door.biz.service.impl;

import com.xique.common.core.web.domain.AjaxResult;
import com.xique.door.bean.domain.*;
import com.xique.door.bean.request.permissionGroup.PermissionGroupSelectUserRequest;
import com.xique.door.biz.service.IPermissionGroupUserBizService;
import com.xique.door.constant.BuildConstant;
import com.xique.door.mapper.BuildHouseMapper;
import com.xique.door.mapper.BuildHouseUserMapper;
import com.xique.door.service.IBuildBuildingService;
import com.xique.door.service.IPermissionGroupService;
import com.xique.door.service.IPermissionGroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: caogq
 * @Date: 2021/9/15 10:43
 * @Description:
 */
@Service
public class BuildHouseUserPermissonBizService {

    @Autowired
    private IPermissionGroupService permissionGroupService;

    @Autowired
    private IPermissionGroupUserBizService permissionGroupUserBizService;

    @Autowired
    private BuildHouseUserMapper buildHouseUserMapper;

    @Autowired
    private BuildHouseMapper buildHouseMapper;

    @Autowired
    private IPermissionGroupUserService permissionGroupUserService;

    @Autowired
    private IBuildBuildingService buildBuildingService;

    public AjaxResult addUserHousePermissGroup(User user) {
        //1、新增用户房屋中间表
        int i = this.addUserHouse(user);
        if (i<1){
            return AjaxResult.error("用户绑定房屋时发生错误!");
        }
        //2、新增用户权限组中间表
        AjaxResult result = this.addUserPermissGroup(user);

        //3、业主更新房屋表业主信息
        if ("0".equals(user.getIdentityType())){
            this.updateHouseOwnerInfo(user);
        }

        return result;
    }



    public int addUserHouse(User user) {
        BuildHouse buildHouse = buildHouseMapper.selectBuildHouseById(user.getHouseId());
        BuildHouseUser buildHouseUser = new BuildHouseUser();
        buildHouseUser.setUserId(user.getId());
        buildHouseUser.setHouseId(user.getHouseId());
        buildHouseUser.setHouseName(buildHouse.getHouseName());
        buildHouseUser.setUserName(user.getUserName());
        buildHouseUser.setDueDate(user.getDueDate());
        buildHouseUser.setRelation(user.getRelation());
        buildHouseUser.setLiveWay(user.getLiveWay());
        buildHouseUser.setCheckInDate(user.getCheckInDate());
        buildHouseUser.setIdentityType(user.getIdentityType());
        buildHouseUser.setBuildingId(buildHouse.getBuildingId());
        buildHouseUser.setBuildingName(buildHouse.getBuildingName());
        user.setBuildingId(String.valueOf(buildHouse.getBuildingId()));
        return buildHouseUserMapper.insertBuildHouseUser(buildHouseUser);

    }

    public AjaxResult addUserPermissGroup(User user) {
        //1、用户楼栋权限组绑定
        PermissionGroup buildingGroup = new PermissionGroup();
        buildingGroup.setParkId(user.getParkId());
        buildingGroup.setForeignId(Long.valueOf(user.getBuildingId()));
        buildingGroup.setForeignType(BuildConstant.BuildType.BUILDING);
        List<PermissionGroup> buildingGroups = permissionGroupService.selectPermissionGroupList(buildingGroup);

        PermissionGroupUser buildingGroupUser = new PermissionGroupUser();
        buildingGroupUser.setUserId(user.getId());
        buildingGroupUser.setGroupId(buildingGroups.get(0).getId());
        List<PermissionGroupUser> buildingGroupUsers = permissionGroupUserService.selectPermissionGroupUserList(buildingGroupUser);
        if (!buildingGroupUsers.isEmpty()) {
            //用户已经绑定楼栋权限组,不需要再次绑定
            return AjaxResult.success();
        }

        AjaxResult buildingResult = permissionGroupUserBizService.addUsers(new PermissionGroupSelectUserRequest(user.getParkId(), buildingGroups.get(0).getId(), user.getId().toString()));
        if (AjaxResult.isError(buildingResult)){
            return buildingResult;
        }

        //2、用户区域权限组绑定
        PermissionGroup areaGroup = new PermissionGroup();
        areaGroup.setParkId(user.getParkId());
        areaGroup.setForeignType(BuildConstant.BuildType.AREA);
        BuildBuilding buildBuilding = buildBuildingService.selectBuildBuildingById(Long.valueOf(user.getBuildingId()));
        areaGroup.setForeignId(buildBuilding.getAreaId());
        List<PermissionGroup> areaGroups = permissionGroupService.selectPermissionGroupList(areaGroup);

        PermissionGroupUser areaGroupUser = new PermissionGroupUser();
        areaGroupUser.setUserId(user.getId());
        areaGroupUser.setGroupId(areaGroups.get(0).getId());
        List<PermissionGroupUser> areaGroupUsers = permissionGroupUserService.selectPermissionGroupUserList(areaGroupUser);
        if (!areaGroupUsers.isEmpty()) {
            //用户已经绑定区域权限组,不需要再次绑定
            return AjaxResult.success();
        }

        AjaxResult areaResult = permissionGroupUserBizService.addUsers(new PermissionGroupSelectUserRequest(user.getParkId(), areaGroups.get(0).getId(), user.getId().toString()));

        return areaResult;
    }

    private int updateHouseOwnerInfo(User user) {
        Long houseId = user.getHouseId();
        if (houseId == null){
            return 0;
        }
        BuildHouse buildHouse = buildHouseMapper.selectBuildHouseById(houseId);
        buildHouse.setContactName(user.getUserName());
        buildHouse.setContactPhone(user.getPhone());
       return  buildHouseMapper.updateBuildHouse(buildHouse);
    }
}
